//
//  WBNavigationController.swift
//  MyWeiBo
//
//  Created by 俞存旭 on 16/9/23.
//  Copyright © 2016年 俞存旭. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {
    
    
    override func viewDidLoad() {
        
        //隐藏NavigationBar
        navigationBar.isHidden = true
        
        
    }
    
    //重写push方法，所有的push 都会调用此方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //如果不是栈底控制器才需要隐藏，根控制器不需要隐藏
        //隐藏底部tabbar
        if childViewControllers.count > 0
        {
            viewController.hidesBottomBarWhenPushed = true
            
            if let vc = viewController as? WBBaseViewController {
                
                var title = "返回"
                //判断控制器级数
                if childViewControllers.count == 1 {
                    //title显示首页标题
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                //取出自定义 navItem,设置左侧按钮为返回按钮
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent),isBack: true)
            }
        }
        

        
        super.pushViewController(viewController, animated: true)
    }
    
    @objc private func popToParent() {
        popViewController(animated: true)
    }

}
